﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Classifiers : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels
        classifierTypesPanel.Visible = false;
        editClassifierTypePanel.Visible = false;
        addNewClassifierTypePanel.Visible = false;

        classifiersPanel.Visible = false;
        addNewClassifierPanel.Visible = false;
        editClassifierPanel.Visible = false;
        #endregion Hide panels

        try
        {
            if (CheckServerObject())
            {
                #region Get Panel Name

                switch (panelName)
                {
                    #region Classifiers and Classifiers Types
                    case "classifierTypesPanel":
                        classifierTypesPanel.Visible = true;

                        DataTable classifiersTypesDT = mServerObject.GetClassifierTypesList();
                        classifiersTypesDT.DefaultView.Sort = "Name ASC";
                        classifierTypesGridView.DataSource = classifiersTypesDT;
                        classifierTypesGridView.DataBind();

                        break;

                    case "addNewClassifierTypePanel":
                        addNewClassifierTypePanel.Visible = true;
                        DataTable typeClassifier = mServerObject.GetClassifierTypesList();
                        mServerObject.fillSelector(parentClassifierTypeDropDownList, typeClassifier, "Name", "Type ID");
                        break;

                    case "editClassifierTypePanel":
                        editClassifierTypePanel.Visible = true;
                        break;

                    case "classifiersPanel":
                        classifiersPanel.Visible = true;

                        if (!IsPostBack)
                        {
                            DataTable clTypesList = mServerObject.GetClassifierTypesList();
                            mServerObject.fillSelector(classifiersTypesDDL, clTypesList, "Name", "Type ID");
                        }

                        int clTypeID = 0;
                        int.TryParse(classifiersTypesDDL.SelectedValue, out clTypeID);

                        DataTable classifiers = mServerObject.GetAllClassifiers(clTypeID);
                        classifiersPanelGridView.DataSource = classifiers;
                        classifiersPanelGridView.DataBind();
                        break;

                    case "addNewClassifierPanel":
                        addNewClassifierPanel.Visible = true;

                        DataTable clTypesList1 = mServerObject.GetClassifierTypesList();
                        mServerObject.fillSelector(addNewClassifierClassifierTypeDDL, clTypesList1, "Name", "Type ID");

                        FillParentClassifier(addNewClassifierClassifierTypeDDL.SelectedValue);

                        break;

                    case "editClassifierPanel":
                        editClassifierPanel.Visible = true;

                        DataTable clTypesList2 = mServerObject.GetClassifierTypesList();
                        mServerObject.fillSelector(editClassifierTypeOfClassifierDDL, clTypesList2, "Name", "Type ID");

                        break;

                    #endregion Classifiers and Classifiers Types
                         
                    default:
                        break;
                }

                #endregion Get Panel Name
            }
        }
        catch (Exception exception)
        {
            mServerObject.LogFatal(exception.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        DataTable userData = (DataTable)Session["UserData"];

        #region Display Panels depending of area
        if (userData != null && userData.Rows.Count > 0)
        {
            if (CheckServerObject())
            {
                if (!IsPostBack)
                {
                    string areaName = areaGUID; //mServerObject.GetAreaName(areaGUID);
                    // area for each of administration entry points
                    switch (areaName)
                    {
                        case "classifierTypes":
                            ShowPanel(classifierTypesPanel.ID);
                            break;

                        case "classifiers":
                            ShowPanel(classifiersPanel.ID);
                            break;

                        default:
                            ShowPanel(string.Empty);
                            mServerObject.InfoText(this, "Please select area from left panel menu");
                            break;
                    }
                }
            }
        }

        #endregion Display Panels depending of area
    }

    #region Classifiers Types

    protected void classifierTypesNewClassifierTypeButton_Click(object sender, EventArgs e)
    {
        addNewClassifierTypeTextBox.Text = string.Empty;
        ShowPanel(addNewClassifierTypePanel.ID);
    }

    protected void classifierTypesGridEditButton_Click(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;

        editClassifierTypeIDLabel.Text = classifierTypesGridView.Rows[index].Cells[0].Text;
        editClassifierTypeDenumireaTextBox.Text = classifierTypesGridView.Rows[index].Cells[1].Text;
        string parentClassifierTypeString = classifierTypesGridView.Rows[index].Cells[2].Text;

        DataTable typeClassifier = mServerObject.GetClassifierTypesList();
        mServerObject.fillSelector(editParentClassifierTypeDropDownList, typeClassifier, "Name", "Type ID");
        if (!string.IsNullOrEmpty(parentClassifierTypeString) && !parentClassifierTypeString.Equals("&nbsp;"))
        {
            if (typeClassifier != null && typeClassifier.Rows.Count > 0)
            {
                DataRow[] selectedRow = typeClassifier.Select("Name ='" + parentClassifierTypeString + "'");
                if (selectedRow != null && selectedRow.Length > 0)
                {
                    string selectedParentID = selectedRow[0]["Type ID"].ToString();
                    editParentClassifierTypeDropDownList.SelectedValue = selectedParentID;
                }
            }
        }

        ShowPanel(editClassifierTypePanel.ID);
    }

    protected void addNewClassifierTypeSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                string denumirea = addNewClassifierTypeTextBox.Text;
                int parentID = 0;
                int.TryParse(parentClassifierTypeDropDownList.SelectedValue, out parentID);

                if (mServerObject.NewClassifierTypes(denumirea, parentID))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save Classifier Type" + ex.Message);
            }
        }
        ClearPanelAddNewClassifierType();
    }

    private void ClearPanelAddNewClassifierType()
    {
        addNewClassifierTypeTextBox.Text = string.Empty;
        parentClassifierTypeDropDownList.SelectedIndex = -1;
        ShowPanel(classifierTypesPanel.ID);
    }

    protected void addNewClassifierTypeCancelButton_Click(object sender, EventArgs e)
    {
        ClearPanelAddNewClassifierType();
    }

    protected void editClassifierTypeSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                int clTypeID = 0;
                int.TryParse(editClassifierTypeIDLabel.Text, out clTypeID);

                string denumirea = editClassifierTypeDenumireaTextBox.Text;

                int parentID = 0;
                int.TryParse(editParentClassifierTypeDropDownList.SelectedValue, out parentID);

                if (mServerObject.UpdateClassifierTypes(clTypeID, denumirea, parentID))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save Classifier Type" + ex.Message);
            }
        }
        ClearPanelEditClassifierType();
        ShowPanel(classifierTypesPanel.ID);
    }

    protected void editClassifierTypeCancelButton_Click(object sender, EventArgs e)
    {
        ClearPanelEditClassifierType();
        ShowPanel(classifierTypesPanel.ID);
    }

    private void ClearPanelEditClassifierType()
    {
        editClassifierTypeIDLabel.Text = string.Empty;
        editClassifierTypeDenumireaTextBox.Text = string.Empty;
        editParentClassifierTypeDropDownList.SelectedIndex = -1;
    }

    /// /////// Classifiers Region
    protected void classifiersTypesDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowPanel(classifiersPanel.ID);
    }

    protected void classifiersPanelNewClassifierButton_Click(object sender, EventArgs e)
    {
        addNewClassifierNameTextBox.Text = string.Empty;
        ShowPanel(addNewClassifierPanel.ID);
    }

    protected void classifiersPanelGridEditButton_Click(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;
        ShowPanel(editClassifierPanel.ID);

        editClassifierTypeOfClassifierDDL.SelectedValue = classifiersPanelGridView.Rows[index].Cells[0].Text;
        editClassifierCodeLabel.Text = classifiersPanelGridView.Rows[index].Cells[1].Text;
        editClassifierNameTextBox.Text = classifiersPanelGridView.Rows[index].Cells[2].Text;
    }

    protected void editClassifierSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                int classifierID = 0;
                int.TryParse(editClassifierCodeLabel.Text, out classifierID);

                string denumirea = editClassifierNameTextBox.Text;

                if (mServerObject.UpdateClassifier(classifierID, denumirea))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save Classifier" + ex.Message);
            }
        }
        ClearPanelEditClassifiers();
        ShowPanel(classifiersPanel.ID);
    }

    protected void addNewClassifierSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                int clTypeID = 0;
                int.TryParse(addNewClassifierClassifierTypeDDL.SelectedValue, out clTypeID);

                string denumirea = addNewClassifierNameTextBox.Text;

                int parentID = 0;
                int.TryParse(addNewParentClassifierDropDownList.SelectedValue, out parentID);

                if (mServerObject.NewClassifier(clTypeID, denumirea, parentID))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save Classifier" + ex.Message);
            }
        }
        ClearPanelAddNewClassifiers();
        ShowPanel(classifiersPanel.ID);
    }

    protected void addNewClassifierCancelButton_Click(object sender, EventArgs e)
    {
        ClearPanelAddNewClassifiers();
        ShowPanel(classifiersPanel.ID);
    }

    private void ClearPanelAddNewClassifiers()
    {
        addNewClassifierClassifierTypeDDL.SelectedIndex = -1;
        addNewClassifierNameTextBox.Text = string.Empty;
        addNewParentClassifierDropDownList.SelectedIndex = -1;
    }

    protected void editClassifierCancelButton_Click(object sender, EventArgs e)
    {
        ClearPanelEditClassifiers();
        ShowPanel(classifiersPanel.ID);
    }

    private void ClearPanelEditClassifiers()
    {
        editClassifierTypeOfClassifierDDL.SelectedIndex = -1;
        editClassifierCodeLabel.Text = string.Empty;
        editClassifierNameTextBox.Text = string.Empty;
    }

    protected void addNewClassifierClassifierTypeDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillParentClassifier(addNewClassifierClassifierTypeDDL.SelectedValue);
    }

    private void FillParentClassifier(string parentString)
    {
        DataTable typeClassifier = mServerObject.GetClassifierTypesList();
        if (typeClassifier != null && typeClassifier.Rows.Count > 0)
        {
            DataRow[] selectedRow = typeClassifier.Select("[Type ID] = " + parentString);
            if (selectedRow != null && selectedRow.Length > 0)
            {
                int parentID = 0;
                int.TryParse(selectedRow[0]["ParentID"].ToString(), out parentID);

                DataTable classifiers = mServerObject.GetAllClassifiers(parentID);

                mServerObject.fillSelector(addNewParentClassifierDropDownList, classifiers, "Name", "Code");
            }
        }
    }
    #endregion ClassifiersTypes


    protected void classifierTypesGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        classifierTypesGridView.PageIndex = e.NewPageIndex;
        ShowPanel(classifierTypesPanel.ID);
    }
    protected void classifiersPanelGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        classifiersPanelGridView.PageIndex = e.NewPageIndex;
        ShowPanel(classifiersPanel.ID);
    }
}